package com.example.spring_seatreservation.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.spring_seatreservation.Bean.R;
import com.example.spring_seatreservation.entity.SeminarRoomReservation;
import com.example.spring_seatreservation.mapper.SeminarRoomReservationMapper;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/seminarRes")
@AllArgsConstructor
public class SeminarRoomResController {

    private SeminarRoomReservationMapper seminarRoomReservationMapper;

    @PostMapping("/addReservation")
    public Map<String, Object> addReservation(@RequestBody SeminarRoomReservation seminarRoomReservation) {
        LambdaQueryWrapper<SeminarRoomReservation> lambdaQueryWrapper = Wrappers.<SeminarRoomReservation>lambdaQuery()
                .eq(SeminarRoomReservation::getReaderId,seminarRoomReservation.getReaderId())
                .eq(SeminarRoomReservation::getRoomId,seminarRoomReservation.getRoomId())
                .eq(SeminarRoomReservation::getOrderstart,seminarRoomReservation.getOrderstart())
                .eq(SeminarRoomReservation::getOrderend,seminarRoomReservation.getOrderend())
                .gt(SeminarRoomReservation::getState,-1);
        Long count = seminarRoomReservationMapper.selectCount(lambdaQueryWrapper);
        if (count.compareTo(0L) > 0) {
            return new R().bad("已有相同的预约").builder();
        }
        seminarRoomReservationMapper.insert(seminarRoomReservation);
        return new R().ok().builder();
    }

    @PostMapping("/sign")
    public Map<String, Object> sign(@RequestBody SeminarRoomReservation seminarRoomReservation) {
        LambdaUpdateWrapper<SeminarRoomReservation> lambdaQueryWrapper = Wrappers.<SeminarRoomReservation>lambdaUpdate()
                .set(SeminarRoomReservation::getState,1).eq(SeminarRoomReservation::getId,seminarRoomReservation.getId());
        seminarRoomReservationMapper.update(seminarRoomReservation,lambdaQueryWrapper);
        return new R().ok().builder();
    }

    @PostMapping("/signOut")
    public Map<String, Object> signOut(@RequestBody SeminarRoomReservation seminarRoomReservation) {
        LambdaUpdateWrapper<SeminarRoomReservation> lambdaQueryWrapper = Wrappers.<SeminarRoomReservation>lambdaUpdate()
                .set(SeminarRoomReservation::getState,-1).eq(SeminarRoomReservation::getId,seminarRoomReservation.getId());
        seminarRoomReservationMapper.update(seminarRoomReservation,lambdaQueryWrapper);
        return new R().ok().builder();
    }

    @GetMapping("/getReservationByUser")
    public Map<String,Object> getReservationByUser(Long userId){
        List<SeminarRoomReservation> list = seminarRoomReservationMapper.getReservationByUser(userId);
        return new R().ok().add("rows", list).builder();
    }

    @PostMapping("/getSemReservation")
    public Map<String,Object> getSemReservation(@RequestBody Map<String, Object> map){
        List<SeminarRoomReservation> list = seminarRoomReservationMapper.getSemReservation(map.get("number").toString(),
                map.get("userName").toString());
        return new R().ok().add("rows", list).builder();
    }

    @PostMapping("/updState")
    public Map<String,Object> updState(@RequestBody Map<String, Object> map){
        LambdaUpdateWrapper updateWrapper = Wrappers.<SeminarRoomReservation>lambdaUpdate()
                .set(SeminarRoomReservation::getState,map.get("state"))
                .eq(SeminarRoomReservation::getId,map.get("id"));
        return new R().ok().builder();
    }
}
